package servlet.huwenyao;

import net.sf.json.JSONObject;
import pojo.Order;
import service.OrderService;
import service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;

@WebServlet(value = "/InsertOrderServlet")
public class InsertOrderServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置返回的格式
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=utf-8");
        //获取修改页面的数据
        String consumerId = request.getParameter("consumerId");
        String wareId = request.getParameter("wareId");
        String remark = request.getParameter("remark");
        String price = request.getParameter("price");
        String address = request.getParameter("address");
        String state = request.getParameter("state");
        String time = request.getParameter("time");
        Order order=new Order();
        order.setConsumerId(Integer.parseInt(consumerId));
        order.setWareId(wareId);
        order.setOrderRemarks(remark);
        order.setOrderPrice(Integer.parseInt(price));
        order.setOrderAddress(address);
        order.setOrderState(Integer.parseInt(state));
        order.setOrderDate(Timestamp.valueOf(time));
        OrderService os = new OrderServiceImpl();
        int bo=os.InseOrder(order);
        JSONObject json=new JSONObject();
        if(bo>0){
            json.put("msg","新增成功");
        }else{
            json.put("msg","新增失败");
        }
        PrintWriter out = response.getWriter();
        out.print(json);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
